Values, Types and Operators in Javascript

22 August 2021

"Javascript is not Java"
JavaScript is a scripting language that enables us to create dynamic content, control media objects, and more.

1. Value in Javascript

Computers usually deal with a lot of data, 30 billion bytes just in working memory and even more for hard disk, to handle these Javascript splits them into chunks called values.

Javascript uses 64 bits to store a single number value, which means it can represent 264 numbers that sum up to more than 18 quintillions. The actual maximum whole number that can be stored is more in the range of 9 quadrillions. Fractional numbers are written in decimal form.

For extraordinarily big or small numbers, we can also use the scientific exponent format represented by e followed by the exponent of the number.

example: 3.997e6 = 3.997 x 106

2. Operators in Javascript

i) Binary Operators:

Operators that require two values to give a valid result are called binary operators.

★ Modulo is used to find the remainder.
example: x%y gives the remainder after dividing x by y.

★ ">" and "<" can be applied to numbers, strings, and non-alphabetic characters. Comparison happens from left to right.

ii) Unary Operators:

typeof():

Gives the type of value.
example:
typeof(5) -> integer
typeof("word") -> string

★ Minus (-) is both binary and unary operator.
example:
-8: used as a unary operator
8-2: used as a binary operator

iii) Logical Operators:

and, or, not are logical operators which reason the boolean.

! ⇒ Logical NOT

It is a unary operator. It flips the value.
example:
! true -> false

&& ⇒ Logical AND

It is a binary operator. It gives result true only when both values are true.When the value on the left converts to false it returns the left value else it will return the value to the right.

|| ⇒ Logical OR

It is a binary operator. It gives the result as true when either of the value is true.This will give the value of left when the left value is true.If the left value is not true, then only it will check the right value and give the result.

iv) Conditional Operator:

It is the only ternary operator, i.e. it operates on 3 values.
example:
true?3:4 = 3

3. Precedence of operators:

Higher to Lower:

When multiple operators of the same precedence occur next to one another, precedence is decided as per the order of appearance from left to right.
We can also add parenthesis which decides precedence.

4. Unicode Standards:

Javascript represents individual characters using the 16 bits format, but Unicode includes twice as many characters, therefore elements like emojis can take up to two character positions.

The backtick notation(also called template literals) can be used to embed other values, for example: `twice of 55 is $(55 * 2)`

When we write something inside ${}, the result will be calculated and converted into a string and replace the ${} part of the string. the above statement would result in "twice of 55 is 110".

5. Strings:

We can indicate strings in javascript using single quotes (' '), double quotes (" "), or backticks (` `).

\ - Escaping a character
\n - newline
\t - tab space


We can concatenate two strings by using the "+" operator.
In strings, upper case has lower precedence than lower case.

6. Three Special Numbers:

Infinity, - Infinity, and NaN

★ Infinity and - Infinity stand for positive and negative infinities respectively.
NaN: NaN stands for Not a Number even though it is a value of numeric type.
example:
0/0, infiinity+infinity, or any other numeric operation which doesn't lead to a meaningful result.
★ This is the only value that is not equal to itself.

7. Boolean

Boolean has two values, true and false Zero, NaN, and "" (empty string) are false for boolean

8. Short Circuit Evaluation

true || x gives true
false && x gives false

9. Empty Values

Null and Undefined

These values are used to denote the absence of meaningful value, even though they are themselves values and carry no information.

10. Type Coercion

When operators are used with the wrong type of value, Javascript will convert them to the type it needs by default, however the result of such operations may not be desired or expected, this is called type coercion.
However to avoid type coercion we use the operator === and !== to ensure that the value and type are checked.

Summary:

We learned the 4 types of values in Javascript: numbers, strings, booleans, and undefined values We can combine and transform these values with operators.